﻿package cn.antscript.airCMS.display.component 
{
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import cn.antscript.airCMS.display.component.btnbase.AbstractIco;
	import cn.antscript.airCMS.display.component.btnbase.IcoBitmapdatas;
	import cn.antscript.model.SqlModel;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.Text;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author 
	 */
	public class EditPanel extends Sprite
	{
		
		
		private var _alert:Label;
		
		private var _label:Label;
		private var _labelTxt:InputText;
		
		private var _content:Label;
		private var _contentTxt:Text;		
		private static var _instance:EditPanel;
		
		
		private var _submitBtn:AbstractIco;
		private var _cancelBtn:AbstractIco;
		
		private var _data:AbstractData;
		
		private var _saved:Boolean=true;
		
		public function EditPanel(privateClass:PrivateClass) 
		{
			_alert = new Label(this, 60, 270, "");
			
			
			_label = new Label(this, 0, 0, "标题");
			_labelTxt = new InputText(this, 60, 0, "");
			_labelTxt.setSize(440, 25);
			_labelTxt.addEventListener(Event.CHANGE, onChange);
			
			_content = new Label(this, 0, 40, "内容");
			_contentTxt = new Text(this, 60, 40, "");
			_contentTxt.addEventListener(Event.CHANGE, onChange);
			
			_submitBtn = new AbstractIco(IcoBitmapdatas.Save, "保存", this, 480, 280);
			_cancelBtn = new AbstractIco(IcoBitmapdatas.Delete, "返回", this, 480, 30);
			
			_submitBtn.addEventListener(MouseEvent.CLICK, onSubmit);
			_cancelBtn.addEventListener(MouseEvent.CLICK, onCancel);
			
		}
		
		private function onChange(e:Event):void 
		{
			_alert.text = "";
			_saved = false;
		}
		
		private function onSubmit(e:MouseEvent):void 
		{
			_data.label = label;
			_data.txt = content;
			WaitBar.getInstance().show(this.stage);
			SqlModel.getInstance().update(_data, updated);
		}
		public function updated():void {
			_alert.text = "保存成功！";
			WaitBar.getInstance().hide();
			_saved = true;
		}
		
		private function onCancel(e:MouseEvent):void 
		{
			if (_saved) {
				hide();
			}
			else {
				SurePanel.getInstance().show("还未保存，确定返回？",this.stage,hide);
			}
			
		}		
		private function hide():void{
			this.visible = false;
		}
		
		public static function getInstance():EditPanel {
			if (_instance == null) {
				_instance = new EditPanel(new PrivateClass);
			}
			return _instance;
		}
		
		
		public function edit(data:AbstractData, container:DisplayObjectContainer = null):void {
			_data = data;
			this.visible = true;
			_alert.text = "";
			
			if (container != null) {
				container.addChild(EditPanel.getInstance());
			}
			EditPanel.getInstance().x = container.stage.stageWidth / 2 - 280;
			EditPanel.getInstance().y = container.stage.stageWidth / 2 - 200;
			
			
			_cancelBtn.x = stage.stageWidth - 50 - EditPanel.getInstance().x;
			_cancelBtn.y = 50 - EditPanel.getInstance().y;
			
			
			
			graphics.clear();
			graphics.beginFill(0x000000, 0.9);
			graphics.drawRect( -EditPanel.getInstance().x, -EditPanel.getInstance().y, container.stage.stageWidth, container.stage.stageHeight);
			graphics.endFill();
			
			_labelTxt.text = String(data.label);
			_contentTxt.text = String(data.txt);
			
		}
		
		public function get label():String {
			return _labelTxt.text;
		}
		public function get content():String {
			return _contentTxt.text;
		}
		
	}

}

class PrivateClass {
	
}